<?php
	include "../inc/dbconn.php";
    //验证token
    function checkToken (){
        // 在函数内引用全局作用域要加global
        global $conn;
        $data = [];
        if(empty($_SERVER['HTTP_TOKEN'])){
                //用户名不存在
                $data["code"] = 3002;
                $data["msg"] = "请登录";
        }else{
             //验证token的有效期
            $token = $_SERVER['HTTP_TOKEN'];
            $sql = "select timeout from division where token='{$token}'";
            $result = $conn->query($sql);
            $res = mysqli_fetch_assoc($result);
            if (time() - $res['timeout'] > 0) {
                //token长时间未使用而过期，需重新登录
                $data["code"] = 3003;
                $data["msg"] = "token过期，请重新登录";
            } else {
                // 还在有效期
                $data["code"] = 3000;
                $data["msg"] = "身份验证通过";
            }
        }
        echo json_encode($data);
    }
    checkToken();
?>